$(function(){$.widget("ui.dialog",$.ui.dialog,{options:{clickOutside:false,clickOutsideTrigger:""},open:function(){var e=$(this.options.clickOutsideTrigger);var t=this;if(this.options.clickOutside){$(document).on("click.ui.dialogClickOutside"+t.eventNamespace,function(n){if($(n.target).css("display")!="none"&&$(n.target).attr("from")!='simpledialog'&&$(n.target).closest($(e)).length==0&&$(n.target).closest($(t.uiDialog)).length==0){t.close()}})}this._super()},close:function(){var e=this;$(document).off("click.ui.dialogClickOutside"+e.eventNamespace);this._super()}})})
function dynamicLoadingCursorRightClickDialog(position,caller,file,loadFilefinish,args){
	if($("[name=dynamic]").find("div[for='"+file+"']").size() == 0)
	{
		var dialog = cursorRightClickDialog(position,caller,"<div id='loadingScreen'></div>");
		var div = $("<div for='"+file+"'></div>");
		$("[name=dynamic]").append(div);
		document.TmpCallerRightClickDialog = caller;
		document.TmpPositionRightClickDialog = position;
		div.load(file,function(responseText, textStatus, XMLHttpRequest) {
			var caller = document.TmpCallerRightClickDialog;
			var position = document.TmpPositionRightClickDialog;
			delete document.TmpCallerRightClickDialog;
			delete document.TmpPositionRightClickDialog;
			dialog.find(".ui-newdialog-content").html(responseText);
			reLocateCursorRightClickDialog(position,caller,dialog);
			if(loadFilefinish!=null) loadFilefinish(dialog,args);
		});
		return dialog;
	}
	else{
		var content = $("[name=dynamic]").find("div[for='"+file+"']").html();
		var dialog = cursorRightClickDialog(position,caller,content);
		if(loadFilefinish!=null) loadFilefinish(dialog,args);
	}
}
function cursorRightClickDialog(position,caller,content){
	var dialog = rightClickOutSideDialog(caller,content);
	dialog[0].position = position;
	dialog[0].caller = caller;
	reLocateCursorRightClickDialog(position,caller,dialog);
	return dialog;
}
function reLocateCursorRightClickDialog(position,caller,dialog){
	if(position==null) position = dialog[0].position;
	if(caller==null) caller = dialog[0].caller;
	var left = 0;
	var top = 0;
	var pointer = "";
	var d = dialog.parents(".ui-dialog:first");
	dialog.find("div[class*=dialogFrom]").remove();
	if(position == "right"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left - dialog.width());
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = (d.height()/2) - 11;
	}
	else if(position == "right,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left - dialog.width());
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = 20;
	}
	else if(position == "right,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left - dialog.width());
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = d.height() - 60;
	}
	else if(position == "left"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left + caller.width() - 10);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = (d.height()/2) - 11;
	}
	else if(position == "left,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left + caller.width() - 10);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = 20;
	}
	else if(position == "left,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left + caller.width() - 10);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = d.height() - 60;
	}
	else if(position == "top"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height()-10);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = (d.width()/2) - 10;
		top = -16;
	}
	else if(position == "top,left"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height()-10);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = 20;
		top = -16;
	}
	else if(position == "top,right"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height()-10);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = d.width() - 40;
		top = -16;
	}
	else if(position == "bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height()+10);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		pointer = $("<div class='dialogFromBottomW'><div>");
		left = (d.width()/2) - 10;
		top = d.height();
	}
	else if(position == "bottom,left"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height()+10);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		pointer = $("<div class='dialogFromBottomW'><div>");
		left = 20;
		top = d.height();
	}
	else if(position == "bottom,right"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height()+10);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		pointer = $("<div class='dialogFromBottomW'><div>");
		left = d.width() - 40;
		top = d.height();
	}
	
	if(pointer != ""){
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		dialog.append(pointer);
		pointer.css("left",left);
		pointer.css("top",top);
	}
	return dialog;
}
function rightClickOutSideDialog(caller,content)
{
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	var msgDialog = $("#BlankDialog").clone();
	msgDialog.attr("type","RightClick");
	msgDialog.dialog({
		resizable : false,
		modal: false,
		clickOutside: true,
		clickOutsideTrigger: caller,
		close: function() { msgDialog.dialog('destroy').remove(); },
	});
	var maxZ = MaxDivZIndex();
	msgDialog.parent().find('.ui-dialog-titlebar').css("display","none");
	msgDialog.parent().css("z-index",maxZ+2);
	msgDialog.parent().next().css("z-index",maxZ+1);
	
	//msgDialog.parent().find('')
	msgDialog.css("padding",0);
	msgDialog.css("margin",0);
	msgDialog.html('<div class="ui-newdialog-titlebar" style="display:none;"><div><span class="colse_icon " style="float:right"></span></div></div>' +
			'<div class="ui-newdialog-content">'+content+'</div><div class="ui-newdialog-button" style="display:none;"><div style="text-align:center;"></div></div>');
	msgDialog.css("min-height","")
	msgDialog.find(".colse_icon").click(function() {msgDialog.dialog("close");});
	return msgDialog;
}